//
//  ByLoginVC.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/18.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByLoginVC.h"

/** 单元格 */
#import "ByLARInputCell.h"          // 输入框单元格

/** 自定义视图 */
#import "ByLARTypeSelectView.h"     // 类型选择视图

/** 控制器 */
#import "ByRegisterVC.h"            // 注册控制器

/** 数据 */
#import "ByLARViewModel.h"          // vm


@interface ByLoginVC () <UITableViewDelegate, UITableViewDataSource, ByLARLoginDelegate>

/** 视图 */
@property (nonatomic, strong) UITableView *table;

/** 数据 */
@property (nonatomic, strong) ByLARViewModel *vm;

@end

@implementation ByLoginVC

#pragma mark - getter
- (ByLARViewModel *)vm {
    if (!_vm) {
        _vm = [[ByLARViewModel alloc] init];
        _vm.loginDelegate = self;
    }
    return _vm;
}

#pragma mark - viewController method
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor cyanColor];
    self.isModal = YES;
    
    [self createLeftBackBarButtonItem];
    
    /** 创建表视图 */
    [self createTableView];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    /** 设置导航栏为半透明 状态栏为白色 */
    [self setNaviAttributeWithTranslucent:YES stateBarLightContent:YES];
    
    ByTabBarController *tabBar = (ByTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    [tabBar hideTabBar];
}


#pragma mark - net requset
- (void)loginSuccess:(ByNetBaseResult *)result {
    [ByCoreToolAlert alertSuccess:result.msg];
    /** 存储登录信息 */
    [ByCommonCache setUserInfo:result.data];
    /** 返回 */
    [self leftAction:nil];
}
- (void)loginFailure:(ByNetBaseResult *)result {
    [ByCoreToolAlert alertError:result.msg];
}

#pragma mark - create view
/** 创建表视图 */
- (void)createTableView {
    
    UIImageView *bgView = [[UIImageView alloc] init];
    bgView.image = [UIImage imageNamed:@"登录背景"];
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.table.backgroundView = bgView;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.table registerClass:[ByLARInputCell class] forCellReuseIdentifier:@"ByLARInputCell"];

}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    /** LOGO */
    if (indexPath.row == 0) {
        CGFloat width = SCREEN_WIDTH * 0.7;
        CGFloat height = SCREEN_WIDTH * 0.7 * 22.0 / 48;
        CGFloat x = (SCREEN_WIDTH - width) / 2;
        UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, width, height)];
        logoView.image = [UIImage imageNamed:@"Logo"];
        [cell.contentView addSubview:logoView];
    }
    /** 用户名输入框 */
    else if (indexPath.row == 1) {
        ByLARInputCell *inputCell = [tableView dequeueReusableCellWithIdentifier:@"ByLARInputCell"];
        inputCell.markImgView.image = [UIImage imageNamed:@"用户"];
        inputCell.inputTF.placeholder = @"用户名";
        cell = inputCell;
    }
    /** 密码输入框 */
    else if (indexPath.row == 2) {
        ByLARInputCell *inputCell = [tableView dequeueReusableCellWithIdentifier:@"ByLARInputCell"];
        inputCell.markImgView.image = [UIImage imageNamed:@"密码"];
        inputCell.inputTF.placeholder = @"密码";
        inputCell.inputTF.secureTextEntry = YES;
        cell = inputCell;
    }
    /** 登录类型选择 */
    else if (indexPath.row == 3) {

        /** 类型选择视图 */
        ByLARTypeSelectView *selectView = [[ByLARTypeSelectView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 0.1, 5, 0, 20)];
        selectView.backgroundColor = [UIColor clearColor];
        selectView.typesAry = @[@"企业", @"学生"];
        selectView.tag = 100;
        [cell.contentView addSubview:selectView];
        
//        /** 忘记密码? */
//        [BoyeViewHandle createButtonInView:cell.contentView config:^(UIButton *button) {
//            button.frame = CGRectMake(selectView.right, selectView.top, SCREEN_WIDTH * 0.9 - selectView.right, selectView.height);
//            button.backgroundColor = [UIColor clearColor];
//            button.titleLabel.font = FONT(14);
//            [button setTitle:@"忘记密码?" forState:UIControlStateNormal];
//            [button setTitleColor:JM_THEME_COLOR forState:UIControlStateNormal];
//            [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
//            [button addTarget:self action:@selector(forgetPwd) forControlEvents:UIControlEventTouchUpInside];
//        }];
    }
    /** 登陆按钮 */
    else if (indexPath.row == 4) {
        [BoyeViewHandle createButtonInView:cell.contentView config:^(UIButton *button) {
            button.frame = CGRectMake(SCREEN_WIDTH * 0.1, 5, SCREEN_WIDTH * 0.8, 40);
            button.backgroundColor = JM_THEME_COLOR;
            button.titleLabel.font = FONT(16);
            [button setTitle:@"登录" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
            button.layer.cornerRadius = button.height / 2;
        }];
    }
    /** 注册按钮 */
    else if (indexPath.row == 5) {
        [BoyeViewHandle createButtonInView:cell.contentView config:^(UIButton *button) {
            button.frame = CGRectMake(SCREEN_WIDTH * 0.1, 5, SCREEN_WIDTH * 0.8, 40);
            button.backgroundColor = [UIColor clearColor];
            button.titleLabel.font = FONT(16);
            [button setTitle:@"注册" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
            button.layer.cornerRadius = button.height / 2;
            button.layer.borderColor = [JM_THEME_COLOR CGColor];
            button.layer.borderWidth = 1;
        }];
    }

    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 22.0 / 48 * SCREEN_WIDTH * 0.7 + 50;
    } else if (indexPath.row == 1) {
        return 50;
    } else if (indexPath.row == 2) {
        return 50;
    } else if (indexPath.row == 3) {
        return 40;
    } else if (indexPath.row == 4) {
        return 50;
    } else if (indexPath.row == 5) {
        return 50;
    }
    return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


#pragma mark - 点击事件
- (void)loginClick {

    ByLARInputCell *usernameCell = [self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    NSString *username = usernameCell.inputTF.text;
    ByLARInputCell *passwordCell = [self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    NSString *password = passwordCell.inputTF.text;
    ByLARInputCell *typeCell = [self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
    ByLARTypeSelectView *selectView = (ByLARTypeSelectView *)[typeCell.contentView viewWithTag:100];
    int type = selectView.selectType;
    
    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.username = username;
    params.password = password;
    params.role = [NSString stringWithFormat:@"%i", type];
    
    [ByCoreToolAlert alertLoading:@"正在登录..."];
    [self.vm login:params];
}
- (void)registerClick {

    ByRegisterVC *vc = [[ByRegisterVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];

}
- (void)forgetPwd {
    NSLog(@"忘记密码");

}



#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
